#!/usr/bin/env python
#
#       transferdefinitions.py
#
#       Copyright 2009 karthikeyan S. <kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

class TransferEvents:

    def __init__(self,baseframe):
        self.Inst=baseframe

    def checkavail(self,side):
        """
        Check the availablity of the item on the side
        """
        alist=[]
        for item in range(side.GetItemCount()):
            alist.append(side.GetItemText(item))
        return alist

    def ftrans(self,event):
        """
        Transfer from available to selected
        """
        for item in range(self.Inst.avail.GetItemCount()) :
            clist=self.checkavail(self.Inst.selc)
            if self.Inst.avail.IsSelected(item) and \
                self.Inst.avail.GetItemText(item) not in clist:
                self.Inst.selc.InsertStringItem\
                    (item,self.Inst.avail.GetItemText(item))
                if len(self.Inst.avail.GetItem(item,1).GetText()) >= 1:
                    self.Inst.selc.SetStringItem(self.Inst.selc.FindItem\
                        (0,self.Inst.avail.GetItemText(item)),1,\
                        self.Inst.avail.GetItem(item,1).GetText())
                else:
                    self.Inst.selc.SetStringItem(self.Inst.selc.FindItem\
                        (0,self.Inst.avail.GetItemText(item)),1,\
                        self.Inst.avail.GetItem(item,2).GetText())
        self.selcavail()

    def revtrans(self,event):
        """
        Transfer from selected to available
        """
        temp=[]
        for item in range(self.Inst.selc.GetItemCount()):
            if self.Inst.selc.IsSelected(item):
                temp.append(self.Inst.selc.GetItemText(item))
        for item in temp:
            self.Inst.selc.DeleteItem(self.Inst.selc.FindItem(0,item))

        self.selcavail()

    def selcavail(self):

        if self.Inst.selc.GetItemCount():
            self.Inst.revtrans.Enable()
        else :
            self.Inst.revtrans.Disable()



